<template>
    <div>
        <!-- header -->
        <el-card class="!border-none" shadow="never">
            <el-alert type="warning" title="温馨提示：管理商品单位" :closable="false" show-icon />
            <el-form
                ref="formRef"
                class="mb-[-16px] mt-[15px]"
                :model="queryParams"
                :inline="true"
                @submit.native.prevent
            >
                <el-form-item label="单位名称">
                    <el-input
                        class="w-[240px] h-[32px]"
                        v-model="queryParams.name"
                        placeholder="请输入"
                        clearable
                        @keyup.enter="resetPage"
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                    <!-- <el-button @click="resetParams">导出</el-button> -->
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <el-button type="primary" @click="handleAdd"> 新增单位 </el-button>

            <el-table size="large" class="mt-[14px]" :data="pager.lists">
                <el-table-column label="单位名称" prop="name" min-width="120" />
                <el-table-column label="排序" prop="sort" min-width="150" />
                <el-table-column label="创建时间" prop="create_time" min-width="150" />
                <el-table-column label="操作" min-width="150" fixed="right">
                    <template #default="{ row }">
                        <el-button type="primary" link @click="handleEdit(row)"> 编辑 </el-button>
                        <el-button
                            v-if="row.root != 1"
                            type="danger"
                            link
                            @click="handleDelete(row.id)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script lang="ts" setup>
import { usePaging } from '@/hooks/usePaging'
import { getUnitList, delUnit } from '@/api/goods'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'

const queryParams = reactive({
    name: ''
})
const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: getUnitList,
    params: queryParams
})
//初始化数据
getLists()
//展开edit
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}
//删除品牌
const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await delUnit({ id })
    getLists()
}
</script>
<style lang="scss" scope>
//TODO
.el-alert--warning.is-light {
    background-color: rgba(246, 248, 255, 1);
    color: rgba(64, 115, 250, 1);
}
</style>
